package cn.beforedark.yygh.hosp.controller.admin;

import cn.beforedark.yygh.common.result.R;
import cn.beforedark.yygh.common.util.MD5;
import cn.beforedark.yygh.dto.hosp.HospitalSetQueryDto;
import cn.beforedark.yygh.hosp.service.HospitalSetService;
import cn.beforedark.yygh.model.hosp.HospitalSet;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

/**
 * /admin/hosp/hospitalSet
 */
@RestController
@RequestMapping("/admin/hosp/hospitalSet")
// @CrossOrigin
public class HospitalSetController {

    @Autowired
    private HospitalSetService hospitalSetService;


    /**
     * 查询所有的医院设置信息
     *
     * @return list
     */
    @GetMapping("/findAll")
    public R findAllHospitalSet() {
        // 调用service的方法
        List<HospitalSet> list = hospitalSetService.list();
        return R.ok().data("items", list);
    }

    /**
     * 根据ID删除对应的医院设置信息
     *
     * @param id id
     * @return flag
     */
    @DeleteMapping("/{id}")
    public R removeHospSet(@PathVariable Long id) {
        boolean flag = hospitalSetService.removeById(id);
        return flag ? R.ok() : R.error();
    }

    /**
     * 带查询条件的分页
     *
     * @param current             current
     * @param limit               limit
     * @param hospitalSetQueryDto hospitalSetQueryDto
     * @return vo
     */
    @PostMapping("/findPageHospSet/{current}/{limit}")
    public R getPageInfo(@PathVariable Integer current,
                         @PathVariable Integer limit,
                         @RequestBody(required = false) HospitalSetQueryDto hospitalSetQueryDto) {

        Page<HospitalSet> page = new Page<>(current, limit);
        QueryWrapper<HospitalSet> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(hospitalSetQueryDto.getHosname())) {
            wrapper.like("hosname", hospitalSetQueryDto.getHosname());
        }
        if (!StringUtils.isEmpty(hospitalSetQueryDto.getHoscode())) {
            wrapper.like("hoscode", hospitalSetQueryDto.getHoscode());
        }
        hospitalSetService.page(page, wrapper);
        return R.ok().data("total", page.getTotal()).data("rows", page.getRecords());
    }

    /**
     * 新增医院信息
     *
     * @param hospitalSet hospitalSet
     * @return vo
     */
    @PostMapping("/save")
    public R save(@RequestBody HospitalSet hospitalSet) {
        hospitalSet.setStatus(1);
        // 设置第三方医院对接密钥
        Random random = new Random();
        hospitalSet.setSignKey(MD5.encrypt(System.currentTimeMillis() + "" + random.nextInt(1000)));
        hospitalSetService.save(hospitalSet);
        return R.ok();
    }

    /**
     * 根据id查询医院信息
     *
     * @param id id
     * @return vo
     */
    @GetMapping("/detail/{id}")
    public R getById(@PathVariable Integer id) {
        return R.ok().data("item", hospitalSetService.getById(id));
    }

    /**
     * 根据id修改医院信息
     *
     * @param hospitalSet hospitalSet
     * @return vo
     */
    @PutMapping("/update")
    public R update(@RequestBody HospitalSet hospitalSet) {
        hospitalSetService.updateById(hospitalSet);
        return R.ok();
    }

    /**
     * 批量删除医院信息
     *
     * @param ids ids
     * @return vo
     */
    @DeleteMapping("/delete")
    public R batchDelete(@RequestBody List<Long> ids) {
        hospitalSetService.removeByIds(ids);
        return R.ok();
    }

    /**
     * 根据id修改医院状态
     *
     * @param id     id
     * @param status status
     * @return vo
     */
    @PutMapping("/status/{id}/{status}")
    public R updateStatus(@PathVariable Long id,
                          @PathVariable Integer status) {
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        hospitalSet.setStatus(status);
        hospitalSetService.updateById(hospitalSet);
        return R.ok();
    }






}

